'use strict';

define(['controllers', '../service/dictService'], function (controllers) {
    controllers.system.register('DictController', ['$scope', 'DictService', function ($scope, dictService) {
        $scope.init = function () {
            $scope.pageNo = 1;
            $scope.pageSize = 10;
            $scope.TYPES = ['boolean', 'int', 'long', 'double', 'string'];
            $scope.LEVEL_SYSTEM = 1;
        };

        $scope.initEvent = function () {
            $('#editDialog').on('shown.bs.modal', function () {
                if ($scope.save_id != null) {
                    $('#save_value').select();
                } else {
                    $('#save_name').select();
                }
            });
            $('#save_value, #save_name, #save_remark').keypress(function (e) {
                if (isSubmitKeyCode(e)) {
                    $scope.save();
                }
            });
        };

        $scope.setEditForm = function (dict) {
            dict = dict == null ? {} : dict;
            $scope.save_id = dict.id;
            $scope.save_name = dict.name;
            $scope.save_remark = dict.remark;
            $scope.save_type = dict.type == null ? 'string' : dict.type;
            $scope.save_value = dict.value;
        };

        $scope.query = function (pageNo) {
            if (pageNo != null) {
                $scope.pageNo = pageNo;
            }
            $scope.paged = dictService.query({
                name: $scope.query_name,
                remark: $scope.query_remark,
                pageNo: $scope.pageNo,
                pageSize: $scope.pageSize
            });
        };
        $scope.edit = function (dict) {
            if (dict != null) {
                $scope.setEditForm(dict);
                $scope.editDialogTitle = '编辑数据字典';
                $('#save_name').prop('disabled', true);
                $('#save_remark').prop('disabled', true);
                $('#save_type').prop('disabled', true);
            } else {
                $scope.setEditForm();
                $scope.editDialogTitle = '创建数据字典';
                $('#save_name').prop('disabled', false);
                $('#save_remark').prop('disabled', false);
                $('#save_type').prop('disabled', false);
            }
            $('#editDialog').modal('show');
        };

        $scope.validValue = function () {
            if (isEmpty($scope.save_value)) {
                $('#editErrorMsg').html('名称不能为空').removeClass('hide');
                $('#save_value_group').addClass('has-error');
                return false;
            } else {
                $('#editErrorMsg').addClass('hide');
                $('#save_value_group').removeClass('has-error');
                return true;
            }
        };

        $scope.save = function () {
            if ($scope.validValue()) {
                dictService.save({
                    id: $scope.save_id
                }, {
                    name: $scope.save_name,
                    remark: $scope.save_remark,
                    type: $scope.save_type,
                    value: $scope.save_value
                }, function () {
                    $('#editDialog').modal('hide');
                    $scope.setEditForm(null);
                    $scope.query();
                });
            }
        };

        $scope.confirmRemove = function (id) {
            $scope.removeId = id;
            $('#removeDialog').modal('show');
        };

        $scope.remove = function () {
            dictService.remove({id: $scope.removeId}, function () {
                $scope.removeId = null;
                $('#removeDialog').modal('hide');
                $scope.query();
            });
        };

        $scope.init();
        $scope.initEvent();
        $scope.query();
    }]);
});